/**
 * 
 */
package cii_lib_graph;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import resources.Rule;
import cii_lib.CCII_ACCESS_HPAR;
import cii_lib.CCII_ACCESS_IPAR;
import cii_lib.CCII_ACCESS_LPAR;
import cii_lib.CCII_ACCESS_SPAR;
import cii_lib.CCII_COMPONENT;

/**
 * @author pdrabik
 *
 */
public class GComponentManager {
	
	private static final Logger log = LoggerFactory
			.getLogger(GComponentManager.class);

	private JTable jTable1 = new JTable();
	private JTable jTable3 = new JTable();
	private CCII_COMPONENT comp;
	
	public GComponentManager() {
		log.debug("empty GComponentManager");
	}
	
	public GComponentManager(CCII_COMPONENT component) {
		comp = component;
		initPanel();
	}

	public JPanel getDefaultPanel() {

		JPanel jCompPanel = new JPanel();
		JScrollPane jScrollPane1 = new JScrollPane();
		JScrollPane jScrollPane2 = new JScrollPane();
		JTable jTable2 = new JTable();
		JPanel jPanel10 = new JPanel();
		GInterfaceTablePanel gii;
		jCompPanel.setBounds(0, 0, 610, 385);
		jCompPanel.setLayout(null);
		jCompPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)), comp.PathName(), TitledBorder.LEADING, TitledBorder.TOP));
		{
			jCompPanel.add(jScrollPane2);
			jScrollPane2.setBounds(28, 21, 273, 175);
			{
				gii = new GInterfaceTablePanel();
				jPanel10 = gii.getPanel();
				jPanel10.setBounds(308, 14, 294, 357);
				jCompPanel.add(jPanel10);
			}
			{
				jTable2 = getTables()[1];
				final GInterfaceTablePanel tmpgii = gii;
				jTable2.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try{
						int ind = evt.getPoint().y/getTables()[1].getRowHeight(); // ! table row high
							tmpgii.DisplayInterface(comp.getInterfaces().get(ind));
						} catch (Exception e) {
							//mouse out of table
						}
						
					}
				});
				jScrollPane2.setViewportView(jTable2);
			}
		}
		{
			jCompPanel.add(jScrollPane1);
			jScrollPane1.setBounds(28, 203, 273, 168);
			{

				jTable1 = getTables()[0];
				jScrollPane1.setViewportView(jTable1);
			}
		}
		jTable1.getColumn(jTable1.getColumnName(0)).setMinWidth(jScrollPane1.getWidth()/2-8);
		jTable1.getColumn(jTable1.getColumnName(1)).setMinWidth(jScrollPane1.getWidth()/2-8);
		jTable2.getColumn(jTable2.getColumnName(0)).setMinWidth(jScrollPane2.getWidth()/2-1);
		jTable2.getColumn("Width").setMinWidth(jScrollPane2.getWidth()/2-1);
		{
			JLabel jLabel1 = new JLabel();
			jCompPanel.add(jLabel1);
			jLabel1.setText("I:");
			jLabel1.setBounds(7, 21, 14, 28);
		}
		{
			JLabel jLabel2 = new JLabel();
			jCompPanel.add(jLabel2);
			jLabel2.setText("P:");
			jLabel2.setBounds(7, 203, 14, 28);
		}
		return jCompPanel;

	}
	
	public void initPanel() {
		 Rule rowView;
		JScrollPane jScrollPane5 = new JScrollPane();
		//ListModel list = new DefaultListModel();
		TableModel tmod = new DefaultTableModel();
		TableModel tmod2 = new DefaultTableModel();
		int compID;
		if (comp==null) compID=-1;
		else compID = comp.GetCompID();
		if (compID==-1) {
			//list = new DefaultListModel();
			tmod = new DefaultTableModel();
			tmod2 = new DefaultTableModel();
			jTable1.setModel(tmod);
			jTable3.setModel(tmod2);
			rowView = null;
			jScrollPane5.setRowHeaderView(rowView);
		} else {
		rowView = new Rule(Rule.VERTICAL, false);
		//list = new DefaultComboBoxModel(stad.getCompTokens(compID));
		Vector<String> tmp = new Vector<String>();
		//Vector<String> tmp2 = new Vector<String>();
		tmp.add("Name");
		tmp.add("Value");
		
		tmod = new DefaultTableModel(tmp,comp.getParams().size());
		tmod2 = new DefaultTableModel(tmp,comp.getInterfaces().size());
		int it1=0, it2=0;
		
		for (int u=0;u<(comp.getParams().size()>comp.getInterfaces().size()?comp.getParams().size():comp.getInterfaces().size());u++) {
			if (u<comp.getParams().size()) {
			tmod.setValueAt(comp.getParams().get(u).Name(),it1,0);
			tmod.setValueAt(comp.getParams().get(u), it1, 1);
			int val = 0;
			if (comp.getParams().get(u) instanceof CCII_ACCESS_IPAR) {
				CCII_ACCESS_IPAR new_name = (CCII_ACCESS_IPAR) comp.getParams().get(u);
				val = new_name.Get();
				tmod.setValueAt(val, it1, 1);
				it1++;
				new_name = null;
			}
			 else if (comp.getParams().get(u) instanceof CCII_ACCESS_SPAR) {
				CCII_ACCESS_SPAR new_name = (CCII_ACCESS_SPAR) comp.getParams().get(u);
				tmod.setValueAt(new_name.Get(), it1, 1);
				new_name=null;
				it1++;
			 }
			 else if (comp.getParams().get(u) instanceof CCII_ACCESS_HPAR) {
				CCII_ACCESS_HPAR new_name = (CCII_ACCESS_HPAR) comp.getParams().get(u);
				tmod.setValueAt(new_name.Get(), it1, 1);
				new_name=null;
				it1++;
			} else if (comp.getParams().get(u) instanceof CCII_ACCESS_LPAR) {
				CCII_ACCESS_LPAR new_name = (CCII_ACCESS_LPAR) comp.getParams().get(u);
				tmod.setValueAt(new_name.Get(), it1, 1);
				new_name=null;
				it1++;
			} else {
			
			it1++;
			 }
			}
			if (u<comp.getInterfaces().size()) {
			tmod2.setValueAt(comp.getInterfaces().get(u).Name(), it2, 0);
			tmod2.setValueAt(comp.getInterfaces().get(u).Width(), it2, 1);
			it2++;
			}
			}
		
		JTable jTable2 = new JTable();
		jTable3.setModel(tmod2);
		jTable2.setModel(tmod);
		jTable1.setAutoResizeMode(0);
		jTable1.setEnabled(false);
		jTable3.setEnabled(false);
		jTable3.setAutoResizeMode(0);
		jTable3.getColumn("Value").setHeaderValue("Width");
		TableColumnModel tcm1 = new DefaultTableColumnModel();
		tcm1 = jTable2.getColumnModel();
		tcm1.getColumn(1).setWidth(20);
		jTable1.setModel(tmod);
		jTable1.setColumnModel(tcm1);
		jTable1.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jTable1.changeSelection(evt.getPoint().y/jTable1.getRowHeight(), 0, false, false);
				String wart = /*jTable1.getValueAt( jTable1.getSelectedRow(),jTable1.getSelectedColumn())+"\t" +*/
					"" +jTable1.getValueAt( jTable1.getSelectedRow(),jTable1.getSelectedColumn()+1);
				JTextField tTf = new JTextField(); tTf.setText(wart); tTf.selectAll();tTf.copy();
			}
		});
		jTable3.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jTable3.changeSelection(evt.getPoint().y/jTable1.getRowHeight(), 0, false, false);
			}
		});
//		jTable1.getColumn(jTable1.getColumnName(0)).setMinWidth(jScrollPane2.getWidth()/2);
//		jTable1.getColumn(jTable1.getColumnName(1)).setMinWidth(jScrollPane2.getWidth()/2);
//		jTable3.getColumn(jTable3.getColumnName(0)).setMinWidth(jScrollPane4.getWidth()/2);
//		jTable3.getColumn(jTable3.getColumnName(1)).setMinWidth(jScrollPane4.getWidth()/2);
		}
	}
	
	public JTable[] getTables() {
		return new JTable[] {jTable1, jTable3};
	}

	public CCII_COMPONENT getComp() {
		return comp;
	}

	public void setComp(CCII_COMPONENT comp) {
		this.comp = comp;
		initPanel();
	}	
}
